<template>
  <div class="form-container">
    <el-form :model="buttonForm" :rules="rules" ref="buttonForm" label-width="100px" class="demo-form">
      <el-form-item label="按钮名称" prop="name">
        <el-input v-model="buttonForm.name"></el-input>
      </el-form-item>
      <el-form-item label="按钮编码" prop="code">
        <el-input v-model="buttonForm.code" :disabled="title === BUTTON_EDIT"></el-input>
      </el-form-item>
      <el-form-item label="pid" prop="menuId">
        <el-input v-model="buttonForm.menuId" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="按钮描述" prop="des">
        <el-input type="textarea" v-model="buttonForm.des"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel" class="cancel-btn function-btn">取  消</el-button>
      <el-button type="primary" @click="sure('buttonForm')" class="function-btn">确  定</el-button>
    </div>
  </div>
</template>

<script>
  import Config from '../../../../config'
  export default {
    name: 'createButton',
    props: {
      form: {
        type: Object,
        default: function () {
          return {
            name: '',
            code: '',
            des: '',
            menuId: '',
            systemId: '7',
            tenantId: '0',
            type: 2,
          }
        }
      },
      title: {
        type: String,
        default: ''
      }
    },
    data () {
      return {
        BUTTON_INSERT: '按钮新增',
        BUTTON_EDIT: '按钮编辑',
        buttonForm: JSON.parse(JSON.stringify(this.form)),
        rules: {
          name: [
            { required: true, message: '请填写按钮名称', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '请填写按钮编码', trigger: 'blur' }
          ],
          des: [
            { required: true, message: '请填写按钮描述', trigger: 'blur' }
          ]
        }
      }
    },
    watch: {
      form (val) {
        this.buttonForm = JSON.parse(JSON.stringify(val))
      }
    },
    methods: {
      cancel () {
        this.$emit('hide', 'cancel')
      },
      sure (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.save()
          } else {
            this.$message(
              {
                message: '格式不正确',
                type: 'warning'
              }
            )
            return false
          }
        })
      },
      /**
       * 保存数据
       * */
      save () {
        let url = `${Config.AUTH_URL}/button/insertOrUpdate`
        const param = this.buttonForm
        console.log(param)
        this.$http.post(url, param)
          .then(data => {
            console.log(data)
            this.$emit('hide', true)
          })
          .catch(e => {
            this.$emit('hide', false)
          })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .form-container {
    position: absolute;
    top: 50%;
    transform: translate(-50%, -50%);
    left: 50%;
    .demo-form {
      width: 340px;
    }
    .dialog-footer {
      height: 28px;
      text-align: center;
      margin-top: 30px;
      .el-button {
        padding: 7px 27px;
      }
    }
  }
</style>
